package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * -----新增收货地址
 */
public class AddAddressActivity extends BaseActivity implements View.OnClickListener {
    private final int FROM_WHEEL_CODE = 642;
    private final int FROM_RAEA_CODE = 652;
    private TextView title;
    private EditText et_name;
    private EditText et_phone;
    private TextView tv_city;
    private EditText tv_area;
    private EditText et_doorplate;

    private String inputName;
    private String inputPhone;
    private String inputCity;
    private String inputArea;
    private String inputDoorplate;

    private String province;
    private String city;
    private String district;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);

        initView();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("新增收货地址");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_save).setOnClickListener(this);
        findViewById(R.id.rl_city).setOnClickListener(this);
        findViewById(R.id.rl_area).setOnClickListener(this);

        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        tv_city = (TextView) findViewById(R.id.tv_city);
        tv_area = (EditText) findViewById(R.id.tv_area);
        et_doorplate = (EditText) findViewById(R.id.et_doorplate);


    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //城市
            case R.id.rl_city:
                intent = new Intent(this, WheelActivity.class);
                startActivityForResult(intent, FROM_WHEEL_CODE);
                overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
                break;

            //区域
            case R.id.rl_area:
                fromraea();
                break;

            //保存
            case R.id.tv_save:
                inputName = et_name.getText().toString();
                inputPhone = et_phone.getText().toString();
                inputCity = tv_city.getText().toString();
                inputArea = tv_area.getText().toString();
                inputDoorplate = et_doorplate.getText().toString();

                if (!"".equals(inputName) && !"".equals(inputCity) && !"".equals(inputArea) &&MyUtils.isMobileNO(inputPhone)) {
                    uploadByxUtils();
                } else {
                    if ("".equals(inputName)) {
                        MyToast("姓名不能为空");
                    } else if (!MyUtils.isMobileNO(inputPhone)) {
                        MyToast("请输入正确手机号");
                    } else if ("".equals(inputCity)) {
                        MyToast("城市不能为空");
                    } else if ("".equals(inputArea)) {
                        MyToast("区域不能为空");
                    }
                }
                break;

            default:
                break;
        }
    }

    private void uploadByxUtils() {
        String addr = inputArea + " "+inputDoorplate;
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("name", inputName);
        map.put("address", addr);
        map.put("province", province);
        map.put("city", city);
        map.put("area", district);
        map.put("phoneNumber", inputPhone);
        showDialog();
        XUtil.Post(ConfigUtil.ADDADDRESS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                System.out.println(result + "address===");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        finish();
                        MyToast(info);
                    }else if ("2".equals(res)) {
                        MyToast("添加失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    private void fromraea() {
        Intent intent = new Intent(AddAddressActivity.this, AreaShowActivity.class);
        startActivityForResult(intent, FROM_RAEA_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FROM_WHEEL_CODE:
                if (resultCode == RESULT_OK) {
                    province = data.getExtras().getString("province");
                    city = data.getExtras().getString("city");
                    district = data.getExtras().getString("district");

                    tv_city.setText(province + city + district);
                }

                break;

            case FROM_RAEA_CODE:
                break;

            default:
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
